home *** CD-ROM | disk | FTP | other *** search
- /* GPS program by Glen Lalonde, glalonde@vnet.ibm.com */
-
-
- #include <processes.h>
- #include <gestaltequ.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
-
- WindowPtr theWindow, statsWindowPtr;
- Rect drawbound,dragbound,limitRect;
- WindowRecord windowRcd, statsWindowRcd;
- EventRecord theEvent;
-
- short selectedProcess =0;
- char autoRefresh =0;
-
- #define maxProcs 50
-
- #define windowWidth 285
- #define stepsPerScreen 10
- #define topInset 10
- #define bottomInset 10
- #define zoomInDir 1
-
- #define appleMenuNum 127
- #define fileMenuNum 128
- #define zoomMenuNum 129
- #define refreshMenuNum 130
-
- ProcessInfoRec InfoRec[maxProcs];
- ProcessInfoRec *(InfoRecPtr[maxProcs]);
- long screenOrg[700];
-
- unsigned long zoomFactor = 1;
-
- RgnHandle updateRgn;
-
- long absBottom, absTop;
- long graphRange, graphBottom, scrollRange;
- long scaleFactor;
- long memSize;
- long windowLength;
- Rect frameRect, ctrlRect;
- unsigned long currentPos;
- MenuHandle fileMenu, zoomMenu, appleMenu, refreshMenu;
- long graphVertOffset =0;
-
-
- long numProcs;
- ControlHandle scrollBarCtl, nextButton, prevButton;
-
-
- /* functions */
- void makeWindow(void);
- void readProcTable(void);
- void sortProcTable(void);
- void drawMemDiagram(void);
- void sizeSetup(void);
- void eventLoop(void);
- void refreshDiagram(int, int);
- void menuEvent(unsigned long);
- void aboutBox();
- void drawStatsWindow(void);
- void mouseDownInStats(int);
- void mouseDownInGraph(int);
- void setupMenus(void);
- void refreshStats(void);
- pascal void updateAction(ControlHandle theControl, int partCode);
- void mouseDownEvent(void);
- void zoom(char direction);
- void aboutBox(void);
- void refreshProcTable(void);
-
-
- Str255 appleTitle = { 1, appleMark };
-
- void setupMenus(void) {
- appleMenu = NewMenu(appleMenuNum, appleTitle);
- fileMenu = NewMenu(fileMenuNum, "\pFILE");
- zoomMenu = NewMenu(zoomMenuNum, "\pZOOM");
- refreshMenu = NewMenu(refreshMenuNum, "\pRefresh");
-
- AppendMenu(appleMenu, "\PAbout GPS...");
- AppendMenu(appleMenu, "\p-");
- AddResMenu(appleMenu, 'DRVR');
- AppendMenu(fileMenu, "\pQuit/Q");
- AppendMenu(zoomMenu, "\pIn/I");
- AppendMenu(zoomMenu, "\pOut/O");
-
-
- AppendMenu(refreshMenu, "\pRefresh Now/R");
- AppendMenu(refreshMenu, "\pAuto Refresh");
-
- InsertMenu(appleMenu,0);
- InsertMenu(fileMenu,0);
- InsertMenu(zoomMenu,0);
- InsertMenu(refreshMenu, 0);
-
- DrawMenuBar();
- }
-
-
- void main(void)
- {
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitCursor();
- InitMenus();
- InitDialogs(NULL);
- PenNormal();
- TextSize(0);
- TextMode(srcOr);
- FlushEvents( everyEvent, 0 );
-
- readProcTable(); /* read all the process information */
- sortProcTable(); /* ensure it is in the correct order */
-
- setupMenus();
-
- windowLength = screenBits.bounds.bottom-50;
- absBottom = windowLength;
- DisableItem(zoomMenu ,2);
-
- Gestalt(gestaltLogicalRAMSize, &memSize);
-
- SetRect(&frameRect, 0, 200,100,400);
-
- sizeSetup();
-
- updateRgn = NewRgn();
-
- makeWindow();
- TextSize(9);
-
- drawStatsWindow();
- drawMemDiagram(); /* draw diagram of memory use */
-
- eventLoop(); /* main event loop */
- }
-
-
- void refreshDiagram(int old, int new) {
- Rect clearRect;
- unsigned long newTop = screenOrg[new];
- unsigned long oldTop = screenOrg[old];
-
- if (old == new) return; /* no need to update */
-
- SetEmptyRgn(updateRgn);
-
- graphVertOffset = newTop;
-
- SetRect(&clearRect, 0, 0, windowWidth-16, +windowLength);
-
- /* scroll the old data in the window */
- ScrollRect(&clearRect, 0, (newTop-oldTop), updateRgn);
-
- InvalRgn(updateRgn); /* indicate part of window that needs update */
-
- drawMemDiagram(); /* draw the conents of the window */
- }
-
-
- void refreshStats(void) {
- Rect theRect;
-
- SetRect(&theRect, 0,0, 200,300);
- FillRect(&theRect,white);
-
- InvalRect(&theRect);
- drawStatsWindow();
- }
-
-
- pascal void updateAction(ControlHandle theControl, int partCode) {
- short current;
- short newCtlValue;
-
- current = GetCtlValue(theControl);
-
- switch(partCode) {
- case inUpButton: newCtlValue = (current == 0 ? 0 : current-1); break;
- case inPageUp:
- newCtlValue = ((current - stepsPerScreen) < 0 ? 0 :
- (current - stepsPerScreen));
- break;
- case inDownButton:
- newCtlValue = (current == scrollRange ? scrollRange :
- current +1);
- break;
- case inPageDown:
- newCtlValue = ((current + stepsPerScreen) > scrollRange ?
- scrollRange : (current + stepsPerScreen));
- break;
- default: ;
- }
-
- if (current != newCtlValue) {
- SetCtlValue(theControl, newCtlValue);
- refreshDiagram(current, GetCtlValue(theControl));
- }
-
- }
-
-
- void mouseDownEvent(void) {
- WindowPtr whichWindow;
- int part;
- ControlHandle whichControl;
-
- part = FindWindow(theEvent.where, &whichWindow);
-
- if (part == inMenuBar) {
- menuEvent(MenuSelect(theEvent.where));
- return;
- }
-
- if (part == inSysWindow) {
- SystemClick(&theEvent, whichWindow);
- return;
- }
-
- if (FrontWindow() != whichWindow) {
- SelectWindow(whichWindow); return;
- }
-
- if (whichWindow == theWindow) mouseDownInGraph(part);
- if (whichWindow == statsWindowPtr) mouseDownInStats(part);
- }
-
-
- void mouseDownInGraph(int thePart) {
- int part;
- ControlHandle whichControl;
- int oldVal, res;
-
- switch(thePart) {
- case inContent:
- case inGrow:
- GlobalToLocal(&theEvent.where);
-
- part = FindControl(theEvent.where, theWindow, &whichControl);
-
- switch(part) {
- case inThumb:
- oldVal = GetCtlValue(scrollBarCtl);
- res = TrackControl(scrollBarCtl, theEvent.where, NULL);
- if (res) refreshDiagram(oldVal, GetCtlValue(scrollBarCtl));
- break;
- case inUpButton:
- case inDownButton:
- case inPageUp:
- case inPageDown:
- TrackControl(scrollBarCtl, theEvent.where, updateAction);
- break;
- default: ;
- }
- break;
- case inGoAway:
- if (TrackGoAway(theWindow, theEvent.where)) ExitToShell();
- break;
- case inDrag: {
- Rect boundsRect;
- SetRect(&boundsRect, 4, 20, 510, 340); /* calc Correct values later */
- DragWindow(theWindow, theEvent.where, &boundsRect);
- }
- break;
-
- default :;
- }
- }
-
-
- void mouseDownInStats(int thePart) {
- int part, res;
- ControlHandle whichControl;
-
- switch(thePart) {
- case inContent:
- case inGrow:
- GlobalToLocal(&theEvent.where);
-
- part = FindControl(theEvent.where, statsWindowPtr, &whichControl);
-
- switch(part) {
- case inButton:
- res = TrackControl(whichControl, theEvent.where, pushButProc);
- if (res == inButton) {
- if (whichControl == prevButton) {
- if (selectedProcess !=0) {
- selectedProcess--;
- refreshStats();
- }
- }
- if (whichControl == nextButton) {
- if(selectedProcess != (numProcs-1)) {
- selectedProcess++;
- refreshStats();
- }
- }
-
- }
- default: ;
- }
- break;
- case inGoAway:
- break;
- case inDrag: {
- Rect boundsRect;
- SetRect(&boundsRect, 4, 20, 510, 340); /* calc Correct values later */
- DragWindow(statsWindowPtr, theEvent.where, &boundsRect);
- }
-
- default :;
- }
- }
-
-
- void zoom(char direction) {
- Rect badRect;
-
- if (direction == zoomInDir) {
- if (zoomFactor < 32) zoomFactor <<=1;
- else return;
- } else {
- if (zoomFactor > 1) zoomFactor >>=1;
- else return;
- }
-
- switch (zoomFactor) {
- case 1: DisableItem(zoomMenu ,2);
- break;
- case 32: DisableItem(zoomMenu ,1);
- break;
- case 2: EnableItem(zoomMenu, 2);
- break;
- case 16: EnableItem(zoomMenu,1);
- break;
- }
-
- absBottom = zoomFactor*windowLength;
-
- sizeSetup();
-
- graphVertOffset = 0;
-
- /* remake control */
- SetPort(&windowRcd.port);
-
- SetRect(&ctrlRect, windowWidth-16, 0, windowWidth, windowLength);
-
- DisposeControl(scrollBarCtl);
- scrollBarCtl = NewControl(theWindow, &ctrlRect, "\pControl", TRUE,
- 0, 0, scrollRange, scrollBarProc, 0);
-
- SetRect(&badRect, 0, 0, windowWidth, windowLength);
- FillRect(&badRect, white);
-
- InvalRect(&badRect);
- drawMemDiagram();
-
- if (FrontWindow() != theWindow) HiliteControl(scrollBarCtl, 255);
- SetPort(FrontWindow());
- }
-
-
- void aboutBox(void) {
- short itemHit;
- DialogPtr aboutPtr;
-
- aboutPtr = GetNewDialog(128, NULL, (void *) -1);
- if (aboutPtr != 0) ModalDialog(NULL, &itemHit);
- DisposDialog(aboutPtr);
- }
-
-
- void refreshProcTable(void) {
- Rect badRect, theRect;
- WindowPtr frontWindow;
-
- frontWindow = FrontWindow();
-
- /* update tables from process manager */
- readProcTable();
- sortProcTable();
-
- /* graph window */
- selectedProcess =0;
- SetCtlValue(scrollBarCtl, 0);
- graphVertOffset = 0;
-
- SetPort(&windowRcd.port);
-
- SetRect(&badRect, 0, 0, windowWidth, windowLength);
- FillRect(&badRect, white);
-
- InvalRect(&badRect);
- drawMemDiagram();
-
- /* now handle the stats window */
- SetPort(statsWindowPtr);
- SetRect(&theRect, 0,0, 200,300);
- FillRect(&theRect,white);
-
- InvalRect(&theRect);
- drawStatsWindow();
-
- /* reset state */
- SetPort(frontWindow);
- }
-
-
- void menuEvent(unsigned long menuSelected) {
- unsigned char theItem = menuSelected & 0xFFFF;
-
- switch(menuSelected>>16) {
- case fileMenuNum:
- if (theItem == 1) { ExitToShell(); }
- break;
-
- case zoomMenuNum:
- zoom(theItem);
- break;
-
- case refreshMenuNum:
- if (theItem == 1) refreshProcTable();
- if (theItem == 2) {
- autoRefresh = !autoRefresh;
- CheckItem(refreshMenu, 2, autoRefresh);
- }
- break;
-
- case appleMenuNum:
- if (theItem == 1) aboutBox();
- else {
- Str255 accName;
- GetItem(appleMenu, theItem, accName);
- OpenDeskAcc(accName);
- }
- default: ;
- }
-
- HiliteMenu(0);
- }
-
-
-
- /* the Main event loop */
- void eventLoop(void) {
- int junk;
- WindowPtr whichWindow;
-
- do {
- junk = WaitNextEvent( everyEvent, &theEvent, 120, NULL );
-
- switch(theEvent.what) {
- case mouseDown: mouseDownEvent();
- break;
- case updateEvt:
- #if 0
- /* this method will cause a large delay in the update of the */
- /* window */
- FindWindow(theEvent.where, &whichWindow);
- if (whichWindow == theWindow) drawMemDiagram();
- if (whichWindow == statsWindowPtr) drawStatsWindow();
- #endif
- /* MUST insure the correct graphPort is set at the end or */
- /* our controls will fail */
- drawMemDiagram();
- drawStatsWindow();
- SetPort(FrontWindow()) ;
- break;
- case keyDown:
- if (theEvent.modifiers && cmdKey)
- menuEvent(MenuKey(theEvent.message & charCodeMask));
- break;
- case activateEvt:
- if (activeFlag & (long)theEvent.modifiers) { /* activate */
- if (theEvent.message == (long)theWindow) {
- HiliteControl(scrollBarCtl, 0);
- }
- if (theEvent.message == (long)statsWindowPtr) {
- HiliteControl(prevButton,0);
- HiliteControl(nextButton,0);
- }
- } else { /* inactivate */
- if (theEvent.message == (long)theWindow) {
- HiliteControl(scrollBarCtl, 255);
- }
- if (theEvent.message == (long)statsWindowPtr) {
- HiliteControl(prevButton,255);
- HiliteControl(nextButton,255);
- }
- }
- SetPort(FrontWindow()); /* this is needed */
- break;
- default: if (autoRefresh) refreshProcTable();
- }
- } while(TRUE);
-
- }
-
-
- void sizeSetup(void) {
- short i;
-
- graphBottom = absBottom-bottomInset;
- graphRange = absBottom - (topInset+bottomInset);
- scaleFactor = (long)memSize/graphRange;
-
- scrollRange = (absBottom/295)*stepsPerScreen;
-
- if (!scrollRange) return;
-
- for(i=0; i<=scrollRange; i++)
- screenOrg[i] = -((long)0xFFFFFFFE & (i*((long)(absBottom)/scrollRange)));
- }
-
-
- void makeWindow(void) {
- Rect boundsRect;
-
- /* first lets do the STATS window and its controls */
- statsWindowPtr = GetNewWindow(128, &statsWindowRcd, (WindowPtr) -1);
-
- SetRect(&boundsRect, 10, 230, 80, 250);
- nextButton = NewControl(statsWindowPtr, &boundsRect,
- "\pNext", TRUE, 0, 0,0, pushButProc, 0);
-
- SetRect(&boundsRect, 110, 230, 180, 250);
- prevButton = NewControl(statsWindowPtr, &boundsRect,
- "\pPrevious", TRUE, 0, 0,0, pushButProc, 0);
-
- /* now the graph window */
- SetRect(&limitRect,05,40,5+windowWidth,40+windowLength);
-
- theWindow = NewWindow (&windowRcd,&limitRect,
- "\pCurrent Processes",1, documentProc,(WindowPtr) -1,1,0);
-
- SetPort(&windowRcd.port);
-
- SetRect(&ctrlRect, windowWidth-16, 0, windowWidth, windowLength);
-
- scrollBarCtl = NewControl(theWindow, &ctrlRect, "\pControl", TRUE,
- 0, 0, scrollRange, scrollBarProc, 0);
- }
-
-
- void drawStatsWindow(void) {
- unsigned char tar[256];
- union { long value;
- char chars[4]; } converter;
-
- BeginUpdate(statsWindowPtr);
-
- SetPort(&statsWindowRcd.port);
-
- TextSize(9);
-
- MoveTo(10, 20); DrawString("\pProcess name: ");
- DrawString(InfoRecPtr[selectedProcess]->processName);
-
- MoveTo(10, 35); DrawString("\pStarting address: ");
- sprintf((char *)tar, "%#lx", InfoRecPtr[selectedProcess]->processLocation);
- CtoPstr((char *)tar);
- DrawString(tar);
-
- MoveTo(10, 50); DrawString("\pLength(size): ");
- sprintf((char *)tar, "%#ld (%ldK)", (long)(InfoRecPtr[selectedProcess]->processSize),
- (long)(InfoRecPtr[selectedProcess]->processSize)/1024);
- CtoPstr((char *)tar);
- DrawString(tar);
-
-
- MoveTo(10, 65); DrawString("\pFree bytes in heap: ");
- sprintf((char *)tar, "%#ld (%ldK)", InfoRecPtr[selectedProcess]->processFreeMem,(InfoRecPtr[selectedProcess]->processFreeMem)/1024);
- CtoPstr((char *)tar);
- DrawString(tar);
-
- MoveTo(10, 80); DrawString("\pProcess Type: ");
- converter.value = InfoRecPtr[selectedProcess]->processType;
- strncpy((char *)tar, converter.chars, 4);
- tar[4]='\0';
- CtoPstr((char *)tar);
- DrawString(tar);
- MoveTo(40, 90);
- if (InfoRecPtr[selectedProcess]->processMode & modeDeskAccessory)
- DrawString("\p(Desk Accessory)");
- if (InfoRecPtr[selectedProcess]->processMode & modeOnlyBackground)
- DrawString("\p(Background-only application)");
-
-
- MoveTo(10, 105); DrawString("\pSignature of app file: ");
- converter.value = InfoRecPtr[selectedProcess]->processSignature;
- strncpy((char *)tar, converter.chars, 4);
- tar[4]='\0';
- CtoPstr((char *)tar);
- DrawString(tar);
-
- MoveTo(10, 120); DrawString("\pAccumulated CPU time: ");
- sprintf((char *)tar, "%#ld", (InfoRecPtr[selectedProcess]->processActiveTime)/60);
- CtoPstr((char *)tar);
- DrawString(tar);
-
-
- MoveTo(10, 135); DrawString("\pSIZE flags:");
- MoveTo(30,145); if (InfoRecPtr[selectedProcess]->processMode & mode32BitCompatible)
- DrawString("\p32 bit compatible");
- MoveTo(30,155); if (InfoRecPtr[selectedProcess]->processMode & modeCanBackground)
- DrawString("\pCan do background processing");
- MoveTo(30,165); if (InfoRecPtr[selectedProcess]->processMode & modeStationeryAware)
- DrawString("\pStationery Aware");
-
-
- DrawControls(statsWindowPtr);
-
- EndUpdate(statsWindowPtr);
- }
-
-
- void readProcTable(void) {
- ProcessSerialNumber process;
- OSErr res;
- ProcessInfoRec recTemp;
-
- process.highLongOfPSN = 0;
- process.lowLongOfPSN = kNoProcess;
-
- numProcs =0;
-
- /* Build up a fake one for the system */
- InfoRec[numProcs].processInfoLength = sizeof(ProcessInfoRec);
- InfoRec[numProcs].processName = "\pSystem";
- InfoRec[numProcs].processAppSpec = NULL;
- InfoRecPtr[numProcs] = &InfoRec[numProcs];
- InfoRec[numProcs].processLocation = *((void **)0x2a6);
- InfoRec[numProcs].processSize =
- (long)(((THz)InfoRec[numProcs].processLocation)->bkLim) -
- (long)(InfoRec[numProcs].processLocation);
-
- numProcs++;
-
- while( GetNextProcess(&process) == noErr) {
- InfoRec[numProcs].processInfoLength = sizeof(ProcessInfoRec);
- if (InfoRec[numProcs].processName == NULL)
- InfoRec[numProcs].processName = (void *)NewPtr(40);
- InfoRec[numProcs].processAppSpec = NULL;
-
- if ((res = GetProcessInformation(&process, &(InfoRec[numProcs]))) != noErr)
- {ExitToShell(); }
- InfoRecPtr[numProcs] = &InfoRec[numProcs];
- numProcs++;
- if (numProcs > maxProcs) break; /* too many to handle */
- }
- }
-
-
- int compare( const void *n1, const void *n2) {
- long diff;
- ProcessInfoRec **p1, **p2;
-
- p1 = (void *)n1;
- p2 = (void *)n2;
-
- diff = ((long)((*p1)->processLocation)) -
- (long)((*p2)->processLocation);
-
- if (diff < 0) return(-1);
- if (diff > 0) return(1);
- return(0);
- }
-
-
- /* Ensure the entires in the proc InfoRec array are sorted by starting
- address */
- void sortProcTable(void) {
- qsort(InfoRecPtr, numProcs, sizeof(char *), compare);
- }
-
-
- void calcPos(short i, long *top, long *bottom) {
-
- *bottom = graphBottom - (long)(InfoRecPtr[i]->processLocation)/scaleFactor;
- *top = *bottom - (InfoRecPtr[i]->processSize)/scaleFactor;
-
- }
-
-
- Pattern *(patternArray[3]) = { &gray, <Gray, &dkGray };
-
- #define screenTop 0
- #define screenBottom windowLength
-
- void drawMemDiagram(void) {
- Rect memRect;
- long top, bottom, sizeInK;
- Str255 memTopStr, sizeStr;
- short i;
-
- BeginUpdate(theWindow);
-
- SetPort(&windowRcd.port);
- PenPat(black);
- PenSize(2,2);
-
- /* draw outline */
- SetRect(&memRect, 20,
- topInset-2+graphVertOffset,
- 100,
- graphBottom+2+graphVertOffset);
-
- FrameRect(&memRect);
-
- PenSize(1,1);
-
- for(i=0; i< numProcs; i++) {
-
- calcPos(i, &top, &bottom);
-
- top += graphVertOffset;
- bottom += graphVertOffset;
-
- if ((bottom >= screenTop) && (top <= screenBottom)) {
- SetRect(&memRect, 22,
- top,
- 98,
- bottom);
-
- FillRect(&memRect, *patternArray[i%3]);
-
- SetRect(&memRect, 20,
- top,
- 100,
- bottom);
-
- FrameRect(&memRect);
-
- /* draw program name */
- MoveTo(110, bottom-((bottom-top)/2));
- DrawString(InfoRecPtr[i]->processName);
- sizeInK = (InfoRecPtr[i]->processSize)/1024;
- NumToString(sizeInK, sizeStr);
- DrawString("\p (");
- DrawString(sizeStr);
- DrawString("\pK)");
- }
- }
-
- MoveTo(110,topInset+4+graphVertOffset);
- NumToString(memSize/1024, memTopStr);
- DrawString(memTopStr);
-
- MoveTo(110, graphBottom+4+graphVertOffset);
- DrawString("\p0");
- DrawControls(theWindow);
-
- EndUpdate(theWindow);
- }